/*
 *
 *  Title:UdpServer.cc
 *  Time:2019-1-5
 *  Athor:Liu
 *
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include <list>
#include <map>

typedef int socketfd;

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        std::cout<<"\nError : Please Input With Port!!"<<std::endl;
        exit(-1);
    }

    std::list<struct sockaddr_in> addrList;//创建一个接收到的的客户端的地址列表
    socketfd sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(sock < 0){
        std::cout<<"\nError : Create Socket Failed!"<<std::endl;
        exit(1);
    } 

    struct sockaddr_in addrServer;
    addrServer.sin_family = AF_INET;
    addrServer.sin_port = htons(atoi(argv[1]));
    addrServer.sin_addr.s_addr = INADDR_ANY;

    if(-1 == bind(sock, (struct sockaddr *)&addrServer, sizeof(addrServer)))
    {
        std::cout<<"Error : Bind Failed!"<<std::endl;
        close(sock);
        exit(2);
    }

    std::cout<<"Start to Recv From Client!"<<std::endl;

    for( ; ; ){
        char recvbuf[10*1024] = {0};
        struct sockaddr_in addrUser1 = {0};
        socklen_t len = sizeof(addrUser1);
        ssize_t ret = recvfrom(sock, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*)&addrUser1, &len);

        if(ret < 0)
        {
            std::cout<<"\nError : Recv From Client Failed!"<<std::endl;
            close(sock);
            exit(3);
        }
        else
        {
            printf("\nInfo : connect ip=[%s] port=[%d]\n",\
                    inet_ntoa(addrUser1.sin_addr), htons(addrUser1.sin_port));

            if(addrList.size() != 0)
            {
                struct sockaddr_in peerAddr = addrList.front();
                socklen_t len2 = sizeof(peerAddr);
                printf("\nInfo : peer ip=[%s] port=[%d]\n", \
                        inet_ntoa(peerAddr.sin_addr), htons(peerAddr.sin_port));

                //现在开始给两个节点互相传递对方的基本信息实现NAT穿越
                if(-1 == sendto(sock, (char*)&addrUser1, len, 0, \
                            (struct sockaddr*)&peerAddr, len2))
                {
                    std::cout<<"\nError : Send to PeerAddr Failed!\n";
                    close(sock);
                    exit(4);
                }
                std::cout<<"\nInfo : Send to PeerAddr Success!\n";

                if(-1 == sendto(sock, (char*)&peerAddr, len2, 0, \
                            (struct sockaddr*)&addrUser1, len))
                {
                    std::cout<<"\nError : Send to AddrUser1 Failed!\n";
                    close(sock);
                    exit(5);
                }
                
                std::cout<<"\nInfo : Send to AddrUser Success!\n";
                addrList.pop_front();
            }
            else
            {
                std::cout<<"\nInfo : List is Empty!\n";
                addrList.push_back(addrUser1);
            }
        }
    }

    return 0;
}


